package com.approval.approvalapp.util.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.MimeTypeMap;
import android.widget.ImageView;
import android.widget.TextView;

import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.SbclckBean;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.DownResponseInterface;

import java.io.Serializable;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

//申报材料查看
public class SbclckActivity extends BaseActivity {

    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.rv)
    RecyclerView rv;
    private List<SbclckBean> sbclckBeanList;

    @Override
    public int getLayoutId() {
        return R.layout.activity_sbclck;
    }


    public static void goInto(Context context, List<SbclckBean> sbclckBeanList) {
        Intent intent = new Intent();
        intent.putExtra("list", (Serializable) sbclckBeanList);
        intent.setClass(context, SbclckActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void initView() {
        sbclckBeanList = (List<SbclckBean>) getIntent().getSerializableExtra("list");
    }

    @Override
    public void initData() {
        rv.setLayoutManager(new LinearLayoutManager(this));
        RvAdapter rvAdapter = new RvAdapter();
        rvAdapter.setSbclckBeanList(sbclckBeanList);
        rv.setAdapter(rvAdapter);

    }


    @OnClick(R.id.back_iv)
    public void onViewClicked() {
        finish();
    }


    class RvAdapter extends RecyclerView.Adapter<RvAdapter.Holder> {

        private List<SbclckBean> sbclckBeanList;

        public void setSbclckBeanList(List<SbclckBean> sbclckBeanList) {
            this.sbclckBeanList = sbclckBeanList;
        }

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(SbclckActivity.this).inflate(R.layout.item_sbclck,parent,false);
            Holder holder = new Holder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.tv.setText(sbclckBeanList.get(position).getFILE_NAME());
            holder.root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final ProgressDialog progressDialog = new ProgressDialog(SbclckActivity.this);
                    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    progressDialog.setProgressNumberFormat(" ");
                    progressDialog.setTitle("正在下载");
                    progressDialog.setMessage("请稍候...");
                    progressDialog.setProgress(0);
                    progressDialog.setMax(100);
                    progressDialog.setCanceledOnTouchOutside(false);
                    progressDialog.show();

                    NetRequsetUtil.getInstance().downLoadFile(SbclckActivity.this,
                            sbclckBeanList.get(position).getFILE_URL(),
                            sbclckBeanList.get(position).getFILE_NAME()
//                                    substring(( sbclckBeanList.get(position).getFILE_URL().lastIndexOf("/") + 1),
//                                            sbclckBeanList.get(position).getFILE_URL().length())
                            , new DownResponseInterface() {
                                @Override
                                public void successFinish() {
                                    progressDialog.dismiss();

                                }

                                @Override
                                public void progress(int progress) {
                                    progressDialog.setProgress(progress);

                                }

                                @Override
                                public void erro() {
                                    progressDialog.dismiss();

                                }
                            });
                }
            });
        }

        @Override
        public int getItemCount() {
            if (sbclckBeanList == null) {
                return 0;
            } else {
                return sbclckBeanList.size();
            }
        }

        class Holder extends RecyclerView.ViewHolder {
            TextView tv;
            ConstraintLayout  root;
            public Holder(View itemView) {
                super(itemView);
                tv = itemView.findViewById(R.id.tv);
                root = itemView.findViewById(R.id.root);
            }
        }
    }
}
